package com.woan.api.servlet;

import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysVideo;
import com.woan.api.model.SysVideoBasic;
import com.woan.api.services.UserClientServices;
import com.woan.api.services.VideoClientServices;
import com.woan.api.util.FuncDate;
import com.woan.api.util.HttpTool;

import net.sf.json.JSONObject;

public class VideoFlv extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpTool ht = new HttpTool();
	protected FuncDate fd = null;

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Hashtable<String, String> hash = null;
		//hash = this.ht.getRequestBody(request);
		System.out.println("视频服务器发送的内容---------" + hash);
		String str1 = null;
		str1 = request.getParameter("type");
		System.err.println("type 222222值是什么。。。。。。" + str1);
		if (str1.isEmpty()) {
			str1 = (String) hash.get("type");
		}
		/*
		 * if (hash==null) { }else { str1 = (String) hash.get("type"); }
		 */
		if (str1.equals("0")) {
			videoFlvStart(request, response);
		}
		// if (str1.equals("1"))
		// {
		// videoStop(request, response);
		// }

		if (str1.equals("2")) {
			videoArchive(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String str1 = request.getParameter("type");
		doGet(request, response);
	}

	public void init() throws ServletException {
	}

	public void videoFlvStart(HttpServletRequest request, HttpServletResponse response)  {
		
		try {
			System.out.println("----------视频FlV直播开始-----------");
			System.out.println("----------视频FlV直播消息内容-----------");
			String groupname = "";
			String username = "";

			String vid = request.getParameter("hashed_id");
			System.err.println("视频vid............" + vid);
			// String user_name = (String) hash.get("author");
			String user_name = request.getParameter("author");
			System.err.println("视频author............" + user_name);
			// String videoPath = (String) hash.get("url");
			String videoPath = request.getParameter("url");
			// String videoSize = (String) hash.get("size");
			System.err.println("视频videoPath............" + videoPath);
			String videoSize = request.getParameter("size");
			System.err.println("视频size............" + videoSize);

			if (user_name.contains("|")) {
				String para[] = user_name.split("\\|");
				groupname = para[0];
				username = para[1];
			}
			ServletContext servletContext = this.getServletContext();
			WebApplicationContext wac = null;
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
			VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
			SysAdmininfo user = null;
			user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
			System.err.println("用户信息：》》》》》》》》》》》》》》》》》》》》》" + user);
			String uid = "";
			/*if (user != null) {
				uid = user.getUid();
				videoClientServices.updateVideoState(uid);
			}*/

			SysVideo sysVideo = new SysVideo();
			sysVideo.setSeqid(System.currentTimeMillis());
			sysVideo.setVid(vid);
			sysVideo.setBeginTime(FuncDate.FindCurSysTime1());
			sysVideo.setEndTime("");
			sysVideo.setPicture("");
			sysVideo.setStatus(0);
			sysVideo.setVideoLength("");
			sysVideo.setVideoPath(videoPath);
			sysVideo.setVideoSize(videoSize);
			sysVideo.setVideoType("android");
			sysVideo.setIsdel("1");
			System.out.println("下面push就开始了！");
			videoClientServices.pushMessageFlv(sysVideo, user.getSysRole().getRid(), groupname, username);
			System.out.println("下面push就结束了！");
			System.out.println("flv直播插入视频");
			videoClientServices.insertVideo_flv(sysVideo,user);
			//videoClientServices.insertVideo(sysVideo, user.getSysRole().getRid(), groupname, username);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		

		// SysVideo sysVideo1 = videoClientServices.listOneVideoinfo(vid, "android");
		/*
		 * System.err.println("视频信息。。。。。。。"+sysVideo1); try { if(sysVideo1==null) { SysVideo sysVideo = new SysVideo(); sysVideo.setVid(vid); sysVideo.setBeginTime(FuncDate.FindCurSysTime1()); sysVideo.setEndTime(""); sysVideo.setPicture(""); sysVideo.setStatus(0); sysVideo.setVideoLength(""); sysVideo.setVideoPath(videoPath); sysVideo.setVideoSize(videoSize); sysVideo.setVideoType("android"); sysVideo.setIsdel("1"); System.out.println("下面push就开始了！"); videoClientServices.pushMessageFlv(sysVideo,user.getSysRole().getRid(),groupname,username); System.out.println("下面push就结束了！"); System.out.println("flv直播插入视频"); videoClientServices.insertVideo(sysVideo,user.getSysRole().getRid(),groupname,username);
		 * 
		 * }else { System.err.println("当前客户有正在直播的视频"); } } catch (Exception e) { e.printStackTrace(); }
		 */

	}

	public void videoStop(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// System.out.println("---------------- FLV视频停止通知 -----------");
		// System.out.println("----------this.hash-----------" + this.hash);
		// String video_code = (String) this.hash.get("hashed_id");
		// String curtime = FuncDate.FindCurSysTime1();
		// VOESysVideo video = new VOESysVideo();
		// BOQVideo boqvideo = new BOQVideo();
		// video = boqvideo.FindvideoByHashid(video_code);
		// video.setVid(video_code);
		// video.setEndtime(curtime);
		// video.setStatusFlv("1");
		// BLVideo blvideo = new BLVideo();
		//
		// String resu = "0";
		// resu = blvideo.updateVideo(video);
		// String msg = "";
		// if (resu.equals("0")) {
		// msg = "0";
		// this.ht.retData(response, msg);
		// } else {
		// msg = "-1";
		// this.ht.retData(response, msg);
		// }
	}

	public void videoArchive(HttpServletRequest request, HttpServletResponse response) {
		try {

			System.out.println("---------------- FLV存档通知 -----------");
			System.out.println("----------FLV存档通知消息内容-----------");
			String vid = request.getParameter("hashed_id");
			System.err.println("视频id" + vid);
			// String user_name = (String) hash.get("author");
			String user_name = request.getParameter("author");
			System.err.println("视频id" + user_name);
			// String videoPath = (String) hash.get("url");
			String videoPath = request.getParameter("url");
			System.err.println("视频url" + videoPath);
			// String videoLength = (String) hash.get("duration");
			String videoLength = request.getParameter("duration");
			System.err.println("视频duration" + videoLength);

			// String task_id=(String)hash.get("service_code");
			// 获取指派任务的Id
			String task_id = request.getParameter("service_code");
			// String task_id=(String)hash.get("service_code");
			System.err.println("任务id"+task_id);
			String curtime = FuncDate.FindCurSysTime1();
			String groupname = "";
			String username = "";
			if (user_name.contains("|")) {
				String para[] = user_name.split("\\|");
				groupname = para[0];
				username = para[1];
			}
			ServletContext servletContext = this.getServletContext();
			WebApplicationContext wac = null;
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			UserClientServices userClientServices = (UserClientServices) wac.getBean("userClientServices");
			VideoClientServices videoClientServices = (VideoClientServices) wac.getBean("videoClientServices");
			SysAdmininfo user = null;
			user = userClientServices.findUserByGroupNameAndUserName(groupname, username);
			List<SysVideo> sysVideo1 = videoClientServices.listVideoinfo(vid, "android");
			System.err.println("查询出的用户信息："+user.getUserName());
			System.out.println("视频信息++++++++++++++++++++++++更新任务了开始+++++++++++++++++++++++++++++++++++++++++++");
			if (!"XZB".equals(task_id)) {
				System.out.println("++++++++++++++++++++++++更新任务了开始进去了———+安卓++++++++++++++++++++++++++++++++++++++++++++");
				//SysVideoBasic videoBasic_task = videoClientServices.getSysvideobasiciDAO().get(task_id);
				SysVideoBasic videoBasic = videoClientServices.getSysvideobasiciDAO().get(vid);
				String title = videoBasic.getTitle();
				String detail = videoBasic.getDetail();
				videoBasic.setDetail(detail);
				videoBasic.setTitle(title);
				// 把任务标题更新到新增的视频中
				videoClientServices.getSysvideobasiciDAO().UpdateVideoBasic(videoBasic);
				// 删除之前的任务。
				//videoClientServices.getSysvideobasiciDAO().deletVideoBasic(videoBasic_task);
				// 根据任务id获取视频播放对象
				//SysVideo video_task = videoClientServices.getSysvideoiDAO().FindVideoByVidAndType(task_id, "android");
				//videoClientServices.getSysvideoiDAO().deletVideo(video_task);
				System.out.println("++++++++++++++++++++++++更新任务结束了+++++++++++++++++++++++++++++++++++++++++++");
			}
			System.out.println("++++++++++++++++++++++++更新任务结束了出来了+++++++++++++++++++++++++++++++++++++++++++");

			if (sysVideo1!=null&&sysVideo1.size() > 0) {
				for (int i = 0; i < sysVideo1.size(); i++) {
					SysVideo sysVideo = sysVideo1.get(i);
					sysVideo.setVideoPath(videoPath);
					sysVideo.setStatus(1);
					sysVideo.setEndTime(FuncDate.FindCurSysTime1());
					sysVideo.setVideoLength(videoLength);
					videoClientServices.updateVideo(sysVideo, user.getSysRole().getRid(), groupname, username);
				}
			}
			System.out.println("flv存档更新视频");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}